package com.syni.mdd.yhd.common.all.utils;

import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.entity.bo.user.CheckUserBo;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsVideoService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @className RequestParamUtils
 * @description 检查参数工具类
 * @Author cfx
 * @DATE 2019/4/11 15:52
 * @VERSION 1.0
 **/
public class RequestParamUtils {
    public static Map<String,Object> checkRequestParams(Map<String,Object> paramsMap,Map<String,Object> needCheckParam){
        if(needCheckParam != null && needCheckParam.size() > 0){
            for(Map.Entry<String,Object> argsEntry : needCheckParam.entrySet()){
                if(!paramsMap.containsKey(argsEntry.getKey())){
                    return ResultMapHelper.failure(argsEntry.getValue().toString());
                }
            }
        }
        return ResultMapHelper.success();
    }

    public static Map<String,Object> checkAuthor(Map<String,Object> paramsMap){
        Map<String,Object> checkResultMap = new HashMap<>();
        RequestHeaderData requestHeaderData = new RequestHeaderData();
        System.out.println("请求头信息：" + requestHeaderData);
        String userToken = "";
        //默认微信端
        int client = 1;
        paramsMap.put("client",client);
        if(requestHeaderData.containsKey("client")){
            client = requestHeaderData.getInteger("client");
            paramsMap.put("client",client);
            if(client == Constants.IOS_CLIENT || client == Constants.ANDROID_CLIENT
                    || client == Constants.SMALL_PROGRAM){
                if(!requestHeaderData.containsKey("token")) {
                    return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,"token失效");
                }
                if(!StringUtils.checkNullAndEmpty(requestHeaderData.containsKey("token"))) {
                    return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,"token失效");
                }
                userToken = requestHeaderData.getString("userid");
                String token = requestHeaderData.getString("token");
                paramsMap.put("virtualUserId",userToken);
                if(!(TokenUtils.checkToken(token,userToken) == TokenUtils.TOKEN_SUCCESS)){
                    return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,"token失效");
                }
                userToken = TokenUtils.getUserThreadLocal().get().toString();
            }
        }else {
            //微信端（做兼容）
            if(!paramsMap.containsKey("open_id")){
                return ResultMapHelper.failure("缺少参数open_id");
            }
            Integer userId = TokenManager.getUserId(paramsMap.get("open_id").toString());
            if(userId == null){
                return ResultMapHelper.failure("用户不存在");
            }
            userToken = userId.toString();
        }
        //是否被冻结
        BmsUser bmsUser = TokenManager.getUser(Integer.parseInt(userToken));
        if (bmsUser == null) {
            return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID, "用户不存在");
        }
        if (bmsUser.getIsFreeze() != null && bmsUser.getIsFreeze() == Constants.COMMON_FLAG_TRUE_INT) {
            return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID, "账号已被冻结");
        }
        checkResultMap.put("status",ResultCode.CODE_NORMAL);
        checkResultMap.put("userIdentify",userToken);
        return checkResultMap;
    }

    public static Map<String,Object> checkAuthor(Integer userId,String token,int client){
        Map<String,Object> checkResultMap = new HashMap<>();
        String userToken = userId.toString();
        //默认微信端
        if(client != Constants.WECHAT_CLIENT){
            if(client == Constants.IOS_CLIENT || client == Constants.ANDROID_CLIENT
                    || client == Constants.SMALL_PROGRAM){
                if(userToken.startsWith(BmsVideoService.visitorPrfix)){
                    return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,"token失效");
                }
                if(!(TokenUtils.checkToken(token,userToken) == TokenUtils.TOKEN_SUCCESS)){
                    return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,"token失效");
                }
            }
        }
        //是否被冻结
        BmsUser bmsUser = TokenManager.getUser(userId);
        if(bmsUser == null){
            return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,"用户不存在");
        }
        if(bmsUser.getIsFreeze() != null && bmsUser.getIsFreeze() == Constants.COMMON_FLAG_TRUE_INT){
            return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,"账号已被冻结");
        }
        checkResultMap.put("status",ResultCode.CODE_NORMAL);
        checkResultMap.put("userIdentify",userToken);
        return checkResultMap;
    }



    public static Map<String,Object> checkParams(Map<String,Object> paramsMap,String...args){
        if(args.length > 0){
            for(String arg : args){
                if(!(paramsMap.containsKey(arg) && StringUtils.checkNullAndEmpty(arg))){
                    return ResultMapHelper.result(ResultCode.CODE_ERROR_03, "业务参数缺失" + arg);
                }
            }
        }
        return ResultMapHelper.success();
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request;
    }

    public static CheckUserBo checkUserBo() {
        CheckUserBo checkUserBo = new CheckUserBo();
        //默认微信端
        int client = RequestHeaderData.getRequestHeaderData().getInteger("client");
        String token = RequestHeaderData.getRequestHeaderData().getString("token");
        if (client == Constants.IOS_CLIENT || client == Constants.ANDROID_CLIENT
                || client == Constants.SMALL_PROGRAM || client == Constants.SMALL_ALI_CLIENT) {
            if (StringUtils.isBlank(token)) {
                return checkUserBo;
            }
            if (!(TokenUtils.checkToken(token) == TokenUtils.TOKEN_SUCCESS)) {
                return checkUserBo;
            }
        } else {//微信端（做兼容）
        }
        //是否被冻结
        String userIdentify;
        if(TokenUtils.getUserThreadLocal().get() == null){
            userIdentify = RequestHeaderData.getRequestHeaderData().getString("userid");
            return new CheckUserBo(userIdentify, token, ResultCode.CODE_NORMAL);
        }
        userIdentify = TokenUtils.getUserThreadLocal().get().toString();
        checkUserBo.setUserId(userIdentify);
        BmsUser bmsUser = TokenManager.getUser(Integer.parseInt(userIdentify));
        if (bmsUser == null) {
            return checkUserBo;
        }
        if (bmsUser.getIsFreeze() != null && bmsUser.getIsFreeze() == Constants.COMMON_FLAG_TRUE_INT) {
            return checkUserBo;
        }
        checkUserBo.setUserId(userIdentify);
        checkUserBo.setCheckStatus(ResultCode.CODE_NORMAL);
        checkUserBo.setToken(token);
        return checkUserBo;
    }
}
